<?PHP
error_reporting(E_ALL);
$relPath="./../../pinc/";
include_once($relPath.'site_vars.php');
require_once $relPath."dpinit.php";
require_once $relPath.'dp_main.inc';

$username = $User->Username();
$projectid = ArgProjectId();
$pagename = ArgPageName();
$roundid = ArgRoundId();

$sql = "SELECT
            '$projectid' AS projectid,
            fileid AS pagename,
            image AS imageurl,
            round1_user AS user1,
            round2_user AS user2,
            round3_user AS user3,
            round4_user AS user4,
            round5_user AS user5,
            round1_time AS time1,
            round2_time AS time2,
            round3_time AS time3,
            round4_time AS time4,
            round5_time AS time5,
            master_text AS text0,
            COALESCE( NULLIF( round1_text, '' ), 
                              master_text ) AS text1,
            COALESCE( NULLIF( round2_text, '' ), 
                      NULLIF( round1_text, '' ),
                              master_text ) AS text2,
            COALESCE( NULLIF( round3_text, '' ), 
                      NULLIF( round2_text, '' ),
                      NULLIF( round1_text, '' ),
                              master_text ) AS text3,
            COALESCE( NULLIF( round4_text, '' ), 
                      NULLIF( round3_text, '' ),
                      NULLIF( round2_text, '' ),
                      NULLIF( round1_text, '' ),
                              master_text ) AS text4,
            COALESCE( NULLIF( round5_text, '' ), 
                      NULLIF( round4_text, '' ),
                      NULLIF( round3_text, '' ),
                      NULLIF( round2_text, '' ),
                      NULLIF( round1_text, '' ),
                              master_text ) AS text5
        FROM
            pages
        WHERE
            projectid = '$projectid'
            and fileid = '$pagename'";

// echo "<br />$sql<br />";
$row = fetch_row( $sql ) ;

switch( $roundid )
{
    case 1 :
        $text0 = $row->text0 ;
        $text1 = $row->text1 ;
        break ;
    case 2 :
        $text0 = $row->text1 ;
        $text1 = $row->text2 ;
        break ;
    case 3 :
        $text0 = $row->text2 ;
        $text1 = $row->text3 ;
        break ;
    case 4 :
        $text0 = $row->text3 ;
        $text1 = $row->text4 ;
        break ;
    case 5 :
        $text0 = $row->text4 ;
        $text1 = $row->text5 ;
        break ;
}

$imageurl = image_url( $row ) ;

// die( "aa $text0 bb $text1 cc" ) ;
echo 
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
<html  lang='en'>
<head>
<title>Text Frame</title>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<meta http-equiv='Pragma' content='no-cache'>
<meta http-equiv='Expires' content='-1'>
<link rel='stylesheet' type='text/css' href='/dp.css'>
<style type='text/css'>
textarea {
  width: 100%;
}
del {
    text-decoration: line-through;
    background-color: cyan;
}
ins {
    text-decoration: underline;
    background-color: gold;
}

.divtext span.mark {
    color:blue
}

pre {
    font-family: monospace;
   font-size: 16px;
}

.divdiff {
    font-family: monospace;
   font-size: 16px;
    padding: 2%;
    float: left;
    width: 50%;
    height: 100%;
}

.divtext {
    display: none;
}

.divimage {
    float: left;
    width: 40%;
    height: 100%;
}

#pgimage {
    width: 100%;
}

</style>
<script type='text/javascript' src='xindiff.js'></script>
<script type='text/javascript'>

var vform ;

function Dictionary() {
    this.map = {};

    this.Add = 
        function(key, value) {
            this.map['_' + key] = value;
        };

    this.Exists =
        function(key) {
            return this.map.hasOwnProperty('_' + key);
        };

    this.Item =
        function(key) {
            return this.map['_' + key];
        };
}

function createMap() {
    return new Dictionary();
}

function doDiff(left, right) {
    var diff = new XinDiffEngine(createMap);
    diff.assign(left.words, 1);
    diff.assign(right.words, 0);

    var output = new XinDiffOutput(createMap);
    output.diff = diff;
    output.symbols = [right.symbols, left.symbols];

    diff.doDiff().serialize(diff, output);
    o = output.getHTML() ;
    $('idView').innerHTML = o ;
    $('text0').style.display = 'none' ;
    $('text1').style.display = 'none' ;
    $('idView').style.display = 'block' ;
}

function diffBy(split) {
    doDiff(split($('text0').innerHTML), 
           split($('text1').innerHTML));
}

function $(id) {
    return document.getElementById(id);
}

function splitCharacters(text) {
    var chars = [];
    var blanks = createMap(true);
    var blank = '';
    var length = text.length;
    for (var i = 0; i < length; ++i) {
        var ch = text.charAt(i);
        switch (ch) {
        case ' ':
        case '\\n':
        case '\\r':
        case '\\t':
            blank += ch;
            break;
        default:
            if (blank != '') {
                blanks.Add(chars.length, blank);
                blank = '';
            }
            chars.push(ch);
            break;
        }
    }
    if (blank != '') {
        blanks.Add(chars.length, blank);
    }
    return {'words': chars, 'symbols': blanks};
}

/*
function splitLines(text) {
    var lines = [];
    var blanks = createMap(true);
    var line = '';
    var blank = '';
    var length = text.length;
    for (var i = 0; i < length; ++i) {
        var ch = text.charAt(i);
        switch (ch) {
        case '\\n':
        case '\\r':
        //case ' ':
        //case '\\t':
            if (line != '') {
                lines.push(line);
                line = '';
            }
            blank += ch;
            break;
        default:
            if (blank != '') {
                blanks.Add(lines.length, blank);
                blank = '';
            }
            line += ch;
            break;
        }
    }
    if (line != '') {
        lines.push(line);
    }
    else if (blank != '') {
        blanks.Add(lines.length, blank);
    }
    return {'words': lines, 'symbols': blanks};
}
*/

function isLiteral(ch) {
    return ((ch <= 'z') && (ch >= 'a')) || ((ch <= 'Z') && (ch >= 'A')) ||
        ((ch <= '9') && (ch >= '0')) || (ch == '_') || (ch == '-') || (ch == '+');
}

function splitWords(text) {
    var words = [];
    var symbols = createMap(true);
    var length = text.length;
    var word = '';
    var symbol = '';
    for (var i = 0; i < length; ++i) {
        if (text.charCodeAt(i) >= 256) {
            if (word != '') {
                words.push(word);
                word = '';
            }
            else if (symbol != '') {
                symbols.Add(words.length, symbol);
                symbol = '';
            }
            words.push(text.charAt(i));
        }
        else {
            var ch = text.charAt(i);
            if (isLiteral(ch)) {
                if (symbol != '') {
                    symbols.Add(words.length, symbol);
                    symbol = '';
                }
                word += ch;
            }
            else {
                if (word != '') {
                    words.push(word);
                    word = '';
                }
                symbol += ch;
            }
        }
    }
    if (word != '') {
        words.push(word);
    }
    else if (symbol != '') {
        symbols.Add(words.length, symbol);
    }
    return {'words': words, 'symbols': symbols};
}

function diffView()
{
    radioView( 'd' ) ;
}

function beforeView()
{
    radioView( 'b' ) ;
}

function afterView()
{
    radioView( 'a' ) ;
}

function radioView( rval )
{
    $('text0').style.display = 'none' ;
    $('text1').style.display = 'none' ;
    $('idView').style.display = 'none' ;
    switch( rval )
    {
        case 'd' :
            $('idView').style.display = 'block' ;
            break ;
        case 'b' :
            $('text0').style.display = 'block' ;
            break ;
        case 'a' :
            $('text1').style.display = 'block' ;
            break ;
    }
}
function init()
{
    diffBy(splitWords);
}

</script>
</head>
<body onLoad='init()'>
<!-- <body> -->
<div>
<form action='' style='margin-top:1em'>
    <input type='button' 
            onclick='diffBy(splitCharacters)' 
            value='Diff by Characters'>
    <input type='button'
            onclick='diffBy(splitWords)'
            value='Diff by Words'>

    <p>View
    <INPUT TYPE='radio' NAME='rview' VALUE='d' onclick='diffView()' CHECKED>Diff
    <INPUT TYPE='radio' NAME='rview' VALUE='b' onclick='beforeView()'>Before
    <INPUT TYPE='radio' NAME='rview' VALUE='a' onclick='afterView()'>After
    </p>

    
    <!--
    <input type='button'
            onclick='diffBy(splitLines)'
            value='Diff by Lines'>
    -->
    [<span class='view'>
                <del>deleted</del> 
                <ins>inserted</ins></span>] 

</form>
</div>
<div class='divimage'>
    <img id='pgimage' 
            title='Page Image' 
            alt='Page Image' 
            src='$imageurl'>
</div>
<pre><div class='divtext' id='text0'>$text0</div></pre>
<pre><div class='divtext' id='text1'>$text1</div></pre>

<div class='divdiff' id='idView'></div>
</body>
</html>\n";

function fetch_row($sql) {
    global $dpdb;
    return $dpdb->SqlOneObject($sql);
}

function image_url( $row )
{
    global $projects_url;
    return $projects_url
            ."/".$row->projectid
            ."/".$row->imageurl;
}

?>
